///===========供应商商品管理.js

//!! 作废
//!!供应商管理移动到 固买市场 -> 市场商品管理 , 产品王令伟在做,   
//!!固买员工说, 后面有可能在商品物料里加个菜单直接跳转到  固买市场-> 市场商品管理 页面


//======= 删除事件 ===========

function delelteRow(evt) {
    console.log("删除行");

    var keys = h3ui.widgets.FormList8.props.selectedRowKeys;

    if (keys.length == 0) {
        h3ui.$message.info("请选择数据");
        return;
    }

    h3ui.$confirm({
        title: '确认删除选择的数据吗',
        onOk() {
            h3ui.models.t_goodses.batchDeleteBO({bizObjectIds: keys}).then(res => {
                h3ui.$message.info("删除成功");
                reloadList("FormList8");
            })
        }
    });


}


//转入客户商城
function turnCustomerMall() {
    h3ui.$message.info('固买测试环境平台该功能未实现,无法参照,删除');
}


//跳转到编辑
function toEditPage() {
    console.log("跳转到编辑")

    var selectKeys = h3ui.widgets.FormList8.props.selectedRowKeys;

    let length = selectKeys.length;
    if (length == 0) {
        h3ui.$info({title: "请选择一行数据"});
        return false;
    }
    if (length > 1) {
        h3ui.$info({title: "仅能选择一行数据"});
        return false;
    }
    // 获取需求池页面url地址
    let url = window.location.href.substring(0, window.location.href.indexOf('?'));
    // 获取需要修改的id
    let demandPoolId = selectKeys[0];


    let pageCode = "goods_detail";

    let param = {
        mode: "update",
        clickcloumn: demandPoolId
    }

    toModePage(pageCode, param);

}

//核心品牌修改
async function updateCore(evt) {
    evt.node.props.disabled = true;

    if (h3ui.widgets.FormList8.props.selectedRow.find(a => a.core == 1)) {
        h3ui.$info({title: "所选产品已包含核心品牌"});
    } else {
        await toCallService("FormList8", "updateGoodsesCore", "核心品牌修改");
    }


    evt.node.props.disabled = false;
}

//启禁用修改
async function updateEnabled(evt) {
    evt.node.props.disabled = true;
    await toCallService("FormList8", "updateGoodsesEnabled", "启禁用修改");
    evt.node.props.disabled = false;
}


//上下架修改
async function updateSaled(evt) {
    evt.node.props.disabled = true;
    await toCallService("FormList8", "updateGoodsesSaled", "上下架修改");
    evt.node.props.disabled = false;
}


//修改领域模型单个属性
function toCallService(formListId, serviceName, msg) {

    var selectKeys = h3ui.widgets[formListId].props.selectedRowKeys;
    let length = selectKeys.length;
    if (length == 0) {
        h3ui.$info({title: "请选择数据"});
        return false;
    }

    h3ui.$confirm({
        title: msg + '确认提交吗',
        onOk() {
            h3ui.services[serviceName]({ids: selectKeys}).then(res => {
                if (res.success) {
                    h3ui.$message.success('提交成功');
                    reloadList(formListId);
                } else {
                    h3ui.$message.error(res.errMessage || '提交失败');
                }
            })
        }
    });
}

//触发列表刷新事件
function reloadList(fromListId) {
    doReloadList({pageIndex: 1, node: {id: fromListId}});
}

//标签页切换 刷新列表, 要求 标签页id 是 "xxx"+"_"+listid
function tabLoadList(evt) {
    doReloadList({node: {id: evt.data.activeKey.split("_")[1]}});
}


/**
 *  跳转表单页面并url携带参数
 *  pageCode // 页面code *
 *  param    // 参数
 *  appCode  // 应用code
 */
function toModePage(pageCode, param, appCode) {
    // 获取页面url地址
    let url = window.location.href.substring(0, window.location.href.indexOf('?'));

    if (appCode) {
        url = url.replaceAll(h3ui.appInfo.appCode, appCode);
    }

    var paramstr = Object.keys(param).map(a => {
        return "&".concat(a).concat("=").concat(param[a])
    }).join("");

    // 跳转创建页面
    window.location.href = url + '?pageCode=' + pageCode + paramstr;
}


// 点击转入客户商城
function clickToShopping(evt) {
    let keysLength = h3ui.widgets.FormList8.props.selectedRowKeys.length;
    if (keysLength == 0) {
        h3ui.$info({title: "请选择一行数据"});
        return false;
    }
    // 显示对话框
    h3ui.widgets.Modal1.props.visible = true;
}

//列表刷新
function doReloadList(evt) {
    var list = h3ui.widgets[evt.node.id];

    var queryId = list.childIds.find(a => a.includes("ModelQueryForm"));

    var filter = h3ui.widgets[queryId].props.value || {};

    filter = JSON.parse(JSON.stringify(filter));

    var pageId = list.childIds.find(a => a.includes("Pagination"));

    var pageIndex = h3ui.widgets[pageId].props.current;
    var pageSize = h3ui.widgets[pageId].props.pageSize;


    list.emit("load", {pageIndex, pageSize, filter});


    list.props.selectedRowKeys = [];
    list.props.selectedRow = [];

}


// 打开 导出选项弹框
function opentExportSelect() {
    var listId = "FormList8";

    var keys = h3ui.widgets[listId].props.selectedRowKeys;
    if (keys.length == 0) {
        h3ui.$message.info("请选择至少一条数据");
        return;
    }

    h3ui.widgets.RadioGroup3.props.listId = listId;
    var modalProps = h3ui.widgets.Modal2.props;
    modalProps.visible = true;
    modalProps.confirmLoading = false;
}

//  导出
function postExport() {
    var modalProps = h3ui.widgets.Modal2.props;
    var listId = h3ui.widgets.RadioGroup3.props.listId;
    var ids = h3ui.widgets[listId].props.selectedRowKeys;

    if (ids.length == 0) {
        h3ui.$message.info("请选择至少一条数据");
        h3ui.widgets.Modal2.props.visible = false;
        return;
    }
    var fileType = h3ui.widgets.RadioGroup3.props.value;
    if (!fileType) {
        h3ui.$message.info("请选择导出类型");
        return;
    }
    modalProps.confirmLoading = true;
    h3ui.services.exportGoodsManageReport({data: {ids, fileType}}).then(res => {
        if (res.success) {
            //调用下载全局弹框
            h3ui.$message.info("导出成功,调用下载全局弹框,等待完善");
            modalProps.visible = false;
            h3ui.widgets[listId].props.selectedRowKeys = [];
            h3ui.widgets[listId].props.selectedRow = [];
        } else {
            h3ui.$message.error(res.errMessage || "导出失败");
        }
        modalProps.confirmLoading = false;
    })
}

// 激活隐藏的导入组件
function uploadFile() {
    var doc = document.getElementById('Upload1');
    doc.getElementsByClassName("wrapper-content-text")[0].click();
    h3ui.widgets.FormList11.props.dataSource = {items: [], total: 0};
}

//
function uploadFileChange(evt) {

    if (evt.data.file.status != "done") return;
    console.log(evt)
    var refId = evt.data.file.response.data.refId
    h3ui.services.vendorGoodsUpload({data: {refId}}).then(res => {
        if (res.success) {
            var item = {items: res.data, total: res.data.length};
            h3ui.widgets.FormList11.props.dataSource = item;
        } else {
            h3ui.$message.error(res.errMessage || "导入失败");
        }
    })
    h3ui.widgets.Upload1.props.value = [];

}

//关闭导入弹框事件
function closeUploadModal() {
    h3ui.widgets.FormList11.props.dataSource = {items: [], total: 0};
}


function cellRender(evt) {
    let {components, col, row, formatText} = evt.data;
    let value = row[col.key];
    let colId = col.id;
    if ('ModelTableColumn101' == colId) {
        //报价次数/参与次数渲染       
        components[0].props.content = row.createdTime + "至" + row.expiry;
    }


}

// 导入商品提交
function vendorGoodsUploadSubmit() {
    debugger;
    var items = JSON.parse(JSON.stringify(h3ui.widgets.FormList11.props.dataSource.items));
    var dataSource = h3ui.models.t_goodses;
    items.forEach(element => {
        dataSource.convert(element, dataSource.schema);
    });

    var param = {data: items, schemaCode: dataSource.schemaCode};

    h3ui.services.vendorGoodsUploadSubmit(param).then(res => {
        if (res.success) {
            h3ui.widgets.CustomModal1.props.visible = false;
            h3ui.widgets.FormList11.props.dataSource = {items: [], total: 0};
            reloadList("FormList8");
        } else {
            h3ui.$message.info(res.errMessage || "提交失败");
        }
    });
}

//导入商品模板下载
function vendorGoodsTempFileDown() {
    window.open("/api/file/download?refId=248cd024a7884051ad9606ea07b6200b", "self");
}